<template>
  <el-dialog v-dialog-drag append-to-body :close-on-click-modal="false" width="1400px" :visible.sync="visible"
    :before-close="beforeClose" :title="title">
    <div class="mainWrapper ub">
      <div class="wrapper ub-f1">
        <el-divider direction="horizontal" content-position="left">质量问题台账</el-divider>
        <SplitForm ref="splitForm" operateType="view" :form="form" :dicts="allDicts" />
      </div>
      <div class="splitLine ub ub-ac ub-pc">
        <img class="icon" src="@/assets/images/arrawRight.jpg" />
      </div>
      <div class="wrapper ub-f1">
        <el-divider direction="horizontal" content-position="left">质量问题库</el-divider>
        <QuestionForm ref="questionForm" :data="transData" />
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submit">同步</el-button>
      <el-button size="mini" @click="beforeClose">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getRevisePeople, getDutyArea } from "@/api/biz/ledger/ledger";
import { getDataList } from "../../qualityFile/api";
import { getTree } from "@/api/biz/enter/enterNext";
import { listType } from "@/api/list/type";
import SplitForm from "./splitForm.vue";
import QuestionForm from "./questionForm.vue";
import { addQuestions} from "@/api/quality/questions";
export default {
  name: "SyncDialog",
  props: {
    visible: Boolean,
    title: {
      type: String,
      default: "同步质量问题库"
    },
    data: {
      type: Object,
      default: () => { }
    },
    dicts: {
      type: Object,
      default: () => { }
    }
  },
  components: { SplitForm, QuestionForm },
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      form: {
        id: "",
        fileId: "", // 质量问题ID
        workNo: "", // 问题编号
        splitTime: "", // 拆分时间
        questionDesc: "", // 问题描述
        supplement: "", // 补充说明
        dutyAreaId: null, // 责任区域
        dutyAreaName: "", // 责任区域
        controlMeasures: "", // 防治措施
        questionLevel: '', // 问题等级
        reworkOrNot: '', // 是否返工
        publishOrNot: '', // 是否公开
        rectificationDeadlineStatus: '', // 整改期限
        questionBeyond: '', // 问题类型
        questionAnalysis: '', // 问题分析
        questionId: null, // 质量问题分类
        questionIdName: '', // 质量问题分类
        reviseTheDates: '', // 整改截止时间
        dataSource: '', // 数据来源
        checkType: null, // 检查类型
        checkPeopleId: '', // 检查人
        revisePeopleId: '', // 整改人
        reviewUserNameId: '', // 复查人
        notifierId: '', // 通知人
        // canDel: "1", // 1可删除，2不可删除
        questionOrigin: "", // 成因
      },
      fileOptions: [],
      dutyAreaOptions: [],
      checkTypeOptions: [],
      questionOptions: [],
      peopleOptions: [],
      libOptions: [], // 质量问题库
      transData: {
        questionInfo: "",
        preventiveMeasures: "",
        questionLevel: "", 
        typeNo: "",
        questionOrigin: "",
      }, // 给问题库的
    };
  },
  created() {
    this.initData();
  },
  computed: {
    allDicts() {
      return {
        ...this.dicts,
        fileOptions: this.fileOptions,
        dutyAreaOptions: this.dutyAreaOptions,
        questionOptions: this.questionOptions,
        peopleOptions: this.peopleOptions,
        checkTypeOptions: this.checkTypeOptions,
        libOptions: this.libOptions,
      }
    }
  },
  methods: {
    //格式化数据
    formatTreeData(data) {
      data.forEach((item) => {
        if (item.children && item.children.length) {
          this.formatTreeData(item.children)
        } else if (item.questionChildren && item.questionChildren.length) {
          item.questionChildren.forEach((subItem) => {
            subItem.label = subItem.dataSource;
          });
          item.children = item.questionChildren
        }
      });
    },
    initData() {
      // 质量问题
      getDataList({ projectId: this.projectId, pageSize: 100000 }).then((data) => {
        this.fileOptions = data.rows.map(item => ({ label: item.fileName, value: item.id }))
      })
      // 整改人，复查人，通知人, 检查人
      getRevisePeople({ projectId: this.projectId, hiddenTrouble: '0' }).then((data) => {
        const res = data.rows.map(item => ({ label: item.nickName, value: item.userId.toString() }))
        this.peopleOptions = res;
      });
      // 责任区域
      getDutyArea({ projectId: this.projectId, hiddenTrouble: '0' }).then((response) => {
        this.dutyAreaOptions = response.data;
      });
      // 检查类型
      getTree({ checkTypeNo: null }).then((response) => {
        this.checkTypeOptions = response.data
      });
      // 质量问题分类
      listType().then((response) => {
        this.questionOptions = this.handleTree(response.data, "typeNo", "parentId");
      });
      Object.keys(this.data).forEach(key => {
        this.$set(this.form, key, this.data[key])
      })
      this.transData.questionInfo = this.form.questionDesc
      this.transData.preventiveMeasures = this.form.controlMeasures
      this.transData.questionLevel = this.form.questionLevel
      this.transData.typeNo = this.form.questionId
      this.transData.questionOrigin = this.form.questionOrigin
    },
    beforeClose() {
      this.$emit('update:visible', false);
    },
    async submit() {
      const formRef = this.$refs.questionForm;
      const res1 = await formRef.validate()
      if (!res1) {
        return;
      }
      const form = formRef.getFormData();
      const res = await addQuestions(form)
      if (res.code == 200) {
        this.$message.success('数据同步成功')
        this.beforeClose()
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.mainWrapper {
  height: 500px;
  overflow-y: auto;
  overflow-x: hidden;

  .wrapper {
    height: 500px;
    // width: 700px;
    overflow-y: auto;
    overflow-x: hidden;
  }

  .splitLine {
    width: 50px;
    height: 100%;
    .icon{
      width: 40px;
      height: 40px;
    }
  }
}
</style>
